#!/bin/sh
# PCP QA Test No. 1872
# Exercise pmsearch REST API endpoints using curl(1).
#
# Copyright (c) 2020 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_check_series
_check_search

_cleanup()
{
    cd $here
    [ -n "$pmproxy_pid" ] && $signal -s TERM $pmproxy_pid
    [ -n "$port" ] && redis-cli $port shutdown
    if $need_restore
    then
        need_restore=false
        _restore_config $PCP_SYSCONF_DIR/pmproxy
        _restore_config $PCP_SYSCONF_DIR/pmseries
    fi
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
signal=$PCP_BINADM_DIR/pmsignal

userid=`id -u`
groupid=`id -g`
username=`id -u -n`
hostname=`hostname`
machineid=`_machine_id`
domainname=`_domain_name`

need_restore=false
$sudo rm -rf $tmp $tmp.* $seq.full
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter_source()
{
    sed \
        -e "s,$here,PATH,g" \
        -e "s,$hostname,QAHOST,g" \
    #end
}

_filter_archive()
{
    pmjson | \
    sed \
        -e "s,bozo-laptop,QALOGHOST,g" \
    #end
}

# real QA test starts here
_save_config $PCP_SYSCONF_DIR/pmproxy
_save_config $PCP_SYSCONF_DIR/pmseries
$sudo rm -f $PCP_SYSCONF_DIR/pmseries/*
$sudo rm -f $PCP_SYSCONF_DIR/pmproxy/*
need_restore=true

echo "=== Start test Redis server ==="
redisport=`_find_free_port`
redispath=`_find_redis_modules`
redisearch="$redispath/redisearch.$DSO_SUFFIX"
$sudo redis-server --port $redisport --loadmodule $redisearch > $tmp.redis 2>&1 &
echo "PING"
pmsleep 0.125
port="-p $redisport"
redis-cli $port ping
echo

# import some well-known test data into Redis
_filter_load()
{
    sed \
	-e "s,$here,PATH,g" \
    #end
}

_filter_elapsed()
{
    sed \
	-e 's/\(\"elapsed\":\) 0\.[0-9][0-9]*/\1 XXX/g' \
    #end
}

_filter_date()
{
    sed \
	-e 's/\(Date:\).*/\1 XXX/g' \
    #end
}

_filter_info()
{
    # These change slightly accross runs.
    # I assume that they are affected by different order of indexing of each
    # record which may build underlying Redis data structures differently
    sed \
	-e 's/\(\"records\":\).*/\1 XXX,/g' \
	-e 's/\(\"bytes_per_record_avg\":\).*/\1 XXX,/g' \
	-e 's/\(\"records_per_doc_avg\":\).*/\1 XXX,/g' \
	-e 's/\(\"offsets_per_term_avg\":\).*/\1 XXX,/g' \
    #end
}

echo "=== Load deterministic search content ==="
pmseries $port --load $here/archives/sample-labels | _filter_load

# start pmproxy
proxyport=`_find_free_port`
proxyopts="-p $proxyport -r $redisport -t" # -Dseries,http,af
pmproxy -f -U $username -x $seq.full -l $tmp.pmproxy.log $proxyopts &
pmproxy_pid=$!

# check pmproxy has started and is available for requests
pmcd_wait -h localhost@localhost:$proxyport -v -t 5sec

# have to wait until everything is properly indexed in Redis
pmsleep 10s

$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep pmproxy >> $seq.full
redis-cli $options keys pcp:text >> $seq.full
cat $tmp.pmproxy.log >> $seq.full

echo "=== /search/text - query for '99' 'random' 'interesting' 'result' ==="
search_terms="99 random interesting result"
for term in $search_terms
do
    url="http://localhost:$proxyport/search/text?query=$term"
    echo "== verify request - $url"
    echo "$url" >> $seq.full
    curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed
done

echo "=== /search/text - no match response ==="

url="http://localhost:$proxyport/search/text?query=bazinga"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

echo "=== /search/text - query param is required ==="

url="http://localhost:$proxyport/search/text"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent -s -o /dev/null -w "%{http_code}\n" "$url" | tee -a $seq.full 

echo "=== /search/text - highlighting is supported on 'name', 'oneline' and 'helptext' by using 'highlight' param ==="

url="http://localhost:$proxyport/search/text?query=domain&highlight=name,oneline,helptext"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=sample&highlight=name"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=counter&highlight=oneline,helptext"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

# url query returns 2 results
echo "=== /search/text  - pagination is supported using cursor params 'limit' and 'offset'"

url="http://localhost:$proxyport/search/text?query=random&limit=1"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=random&limit=1&offset=1"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

echo "=== /search/text - its possible to query only specific fields (name, oneline, helptext, indom) using 'field' param ==="

url="http://localhost:$proxyport/search/text?query=99&field=name"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=99&field=oneline"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=99&field=helptext"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

echo "=== /search/text - filtering out fields from response (name, oneline, helptext, type, indom) using 'return' param ==="

url="http://localhost:$proxyport/search/text?query=99&return=name"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=99&return=type"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=99&return=oneline"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=99&return=helptext"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=99&return=indom"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=random&return=name,type,indom"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

echo "=== /search/text - filtering based on entity type (metric, instance, indom) using 'type' param ==="

url="http://localhost:$proxyport/search/text?query=random&type=metric"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=random&type=indom"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=random&type=instance"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/text?query=random&type=metric,indom"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

echo "=== /search/suggest - generic request ==="

url="http://localhost:$proxyport/search/suggest?query=sam"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/suggest?query=sample.r"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

echo "=== /search/suggest - limit is supported"

url="http://localhost:$proxyport/search/suggest?query=sam&limit=2"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

echo "=== /search/suggest - query param is required ==="

url="http://localhost:$proxyport/search/suggest"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent -s -o /dev/null -w "%{http_code}\n" "$url" | tee -a $seq.full 

echo "=== /search/suggest - no match response ==="

url="http://localhost:$proxyport/search/suggest?query=xxx"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

echo "=== /search/indom - generic request ==="

url="http://localhost:$proxyport/search/indom?query=29.1"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/indom?query=29.3"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

echo "=== /search/indom - no match response ==="

url="http://localhost:$proxyport/search/indom?query=x"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

echo "=== /search/indom - query param is required ==="

url="http://localhost:$proxyport/search/indom"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent -s -o /dev/null -w "%{http_code}\n" "$url" | tee -a $seq.full 

echo "=== /search/indom  - pagination is supported using cursor params 'limit' and 'offset'"

url="http://localhost:$proxyport/search/indom?query=29.1&limit=2&offset=0"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

url="http://localhost:$proxyport/search/indom?query=29.1&limit=2&offset=2"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_elapsed

echo "=== /search/info ==="

url="http://localhost:$proxyport/search/info"
echo "== verify request - $url"
echo "$url" >> $seq.full
curl --get --silent "$url" | tee -a $seq.full | pmjson | _filter_info

# success, all done
status=0
exit
